﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace ThunderboltCustomControlLibrary;

public class AccountComboBox:ComboBox
{
	static AccountComboBox()
	{
		DefaultStyleKeyProperty.OverrideMetadata(typeof(AccountComboBox), new FrameworkPropertyMetadata(typeof(AccountComboBox)));
	}
	#region 依赖属性

	/// <summary>
	///下拉框右侧的图标
	/// </summary>       
	[Description("Gets or sets the icon on the right of the drop-down box")]
	[Category("Common Properties")]
	public Geometry Icon
	{
		get { return (Geometry)GetValue(IconProperty); }
		set { SetValue(IconProperty, value); }
	}

	// Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
	public static readonly DependencyProperty IconProperty =
		DependencyProperty.Register("Icon", typeof(Geometry), typeof(AccountComboBox));


	/// <summary>
	///提示文本
	/// </summary>       
	[Description("Gets or sets  prompt text")]
	[Category("Common Properties")]
	public string PlaceHolder
	{
		get { return (string)GetValue(PlaceHolderProperty); }
		set { SetValue(PlaceHolderProperty, value); }
	}
	public static readonly DependencyProperty PlaceHolderProperty =
		DependencyProperty.Register("PlaceHolder", typeof(string), typeof(AccountComboBox), new PropertyMetadata(string.Empty));

	//图标尺寸
	public double IconSize
	{
		get { return (double)GetValue(IconSizeProperty); }
		set { SetValue(IconSizeProperty, value); }
	}

	// Using a DependencyProperty as the backing store for IconSize.  This enables animation, styling, binding, etc...
	public static readonly DependencyProperty IconSizeProperty =
		DependencyProperty.Register("IconSize", typeof(double), typeof(AccountComboBox), new PropertyMetadata(25d));
	public override void OnApplyTemplate()
	{
		base.OnApplyTemplate();

		TextBox editableTxt = this.Template.FindName("PART_EditableTextBox", this) as TextBox;

		this.Text = PlaceHolder;
		this.Opacity = 0.4;

		editableTxt.MouseDown += (s, e) =>
		{
			this.Text = "";
		};
		editableTxt.TextChanged += (s, e) =>
		{
			this.Text = editableTxt.Text;
		};

		this.LostFocus += (s, e) =>
		{
			if (this.Text.Length == 0)
			{
				this.Text = PlaceHolder;
				this.Opacity =0.4;
			}
			else
			{
				editableTxt.Visibility = Visibility.Visible;
				this.Opacity = 1;
			}
		};

		this.GotFocus += (s, e) =>
		{
			if (this.Text == PlaceHolder)
			{
				this.Text = "";
			}
			editableTxt.Visibility = Visibility.Visible;
		};
	}

	
}
	#endregion
